//
//  UIView+Gradient.m
//  xiacai_ios_v6
//
//  Created by 王振标 on 2018/4/3.
//  Copyright © 2018年 王振标. All rights reserved.
//

#import "UIView+Gradient.h"
#import "UIColor+Hex.h"

@implementation UIView (Gradient)

- (CAGradientLayer *)wzb_addGradient:(NSArray *)colors
{
    return [self wzb_addGradient:colors startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1)];
}
- (CAGradientLayer *)wzb_addGradient:(NSArray *)colors startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint
{
    return [self wzb_addGradient:colors startPoint:startPoint endPoint:endPoint frame:self.bounds];
}
- (CAGradientLayer *)wzb_addGradient:(NSArray *)colors startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint frame:(CGRect)frame
{
    NSMutableArray *array = [NSMutableArray array];
    for (id color in colors) {
        if ([color isKindOfClass:NSString.class]) {
            [array addObject:(id)[UIColor colorWithHexString:color].CGColor];
        } else {
            [array addObject:color];
        }
    }
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = array;
    gradientLayer.startPoint = startPoint;
    gradientLayer.endPoint = endPoint;
    gradientLayer.frame = frame;
    [self.layer insertSublayer:gradientLayer atIndex:0];
    return gradientLayer;
}

@end
